//
//  harvestAddress.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/22.
//  Copyright © 2016年 wl. All rights reserved.
//

import SVProgressHUD

class harvestAddressVC: UIViewController, UITextFieldDelegate {
    var id : Int = 0
    var type: String = ""
    var msg: regiestInfo?{
        didSet{
            
        }
    }
   
    @IBOutlet weak var address: UITextField!
    @IBOutlet weak var postCode: UITextField!
    @IBOutlet weak var name: UITextField!
    @IBOutlet weak var tel: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
//       self.address.delegate = self
//        self.postCode.delegate = self
//        self.name.delegate = self
//        self.tel.delegate = self

        
        
        
    }
    
    override func viewWillDisappear(animated: Bool) {
        //SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
//    func textFieldDidEndEditing(textField: UITextField){
//        if textField == self.address{
//            
//            NSUserDefaults.standardUserDefaults().setObject(self.address!.text!, forKey: address_key)
//            NSUserDefaults.standardUserDefaults().synchronize()
//        }
//        if textField == self.postCode{
//            
//             NSUserDefaults.standardUserDefaults().setObject(self.postCode!.text!, forKey: postCode_key)
//             NSUserDefaults.standardUserDefaults().synchronize()
//        }
//        if textField == self.name{
//            
//           NSUserDefaults.standardUserDefaults().setObject(self.name!.text!, forKey: name_key)
//            NSUserDefaults.standardUserDefaults().synchronize()
//        }
//        if textField == self.tel{
//            NSUserDefaults.standardUserDefaults().setObject(self.tel.text!, forKey: tel_key)
//            
//            NSUserDefaults.standardUserDefaults().synchronize()
//        }
//        
//    }
    @IBAction func getIntergatNum(sender: UIButton){
        if self.address.text == "" {
            SVProgressHUD.showErrorWithStatus("请填写地址")
            return
        }
        if self.postCode.text == "" {
            SVProgressHUD.showErrorWithStatus("请填写地址")
            
            return
            
        }
        if self.name.text == "" {
            SVProgressHUD.showErrorWithStatus("请填写姓名")
            
            return
        }
        if self.tel.text == "" {
            SVProgressHUD.showErrorWithStatus("请填写电话")
            
            return
        }
        self.saveAddress()
//        if self.type == "积分"{
//            self.saveAddress()
//           // self.checkIntegralNum()
//        }else{
//            self.saveAddress()
//        }
    }
    
    
    //MARK:networking
    func saveAddress(){
        var urlStr = String(format: "%@activity/useradd/dosave",BaseUrl)
        if self.type == "积分" {
            urlStr = String(format: "%@mall/dosave", BaseUrl)
        }
        DataTool.saveAddress(urlStr,address: self.address.text!, postcode: self.postCode.text!, name: self.name.text!, tel: self.tel.text!,id: String(self.id),completionHandler: { (newModel) in
            guard newModel != nil else {
                return
            }
            self.msg = newModel
            SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
            if self.type != "积分" {
                let sb = UIStoryboard.init(name: "Activity", bundle: nil)
                
                let vc = sb.instantiateViewControllerWithIdentifier("ActivityApplicationVC") as! ActivityApplicationVC
                
                vc.id = String(self.id)
                vc.nameStr = self.name.text
                vc.addressStr = self.address.text
                vc.postcodeStr = self.postCode.text
                vc.telStr = self.tel.text
                
                self.navigationController?.pushViewController(vc, animated: true)
                
                
            } else {
                
                self.navigationController?.popToRootViewControllerAnimated(true)
            }
            
        })
    }
    func checkIntegralNum(){
        DataTool.checkIntegral(String(id), completionHandler:
            { (newModel) in
                guard let newDataes = newModel else {
                    return
                }
                print(newDataes)
                if newDataes.msg == "保存成功"{
                    SVProgressHUD.showSuccessWithStatus(newDataes.msg)
                    self.navigationController?.popToRootViewControllerAnimated(true)
                }else{
                    SVProgressHUD.showErrorWithStatus(newDataes.msg)
                }
        })

    }

}
